/**
 * 
 */
package com.gmail.blackdog1987.onedollar.service.impl;

import com.gmail.blackdog1987.onedollar.dao.*;
import com.gmail.blackdog1987.onedollar.entity.*;
import com.gmail.blackdog1987.onedollar.entity.base.Page;
import com.gmail.blackdog1987.onedollar.service.CartService;
import com.gmail.blackdog1987.onedollar.service.GoodsService;
import com.gmail.blackdog1987.onedollar.util.JsonChangeUtil;
import com.gmail.blackdog1987.onedollar.vo.GoodsJudgeVO;
import com.gmail.blackdog1987.onedollar.vo.GoodsOneDollarVO;
import com.gmail.blackdog1987.onedollar.vo.GoodsOneDollarWinnerVO;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.sql.Timestamp;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

/**
 * 
 * @author 董国超
 *
 */
@Service("cartService")
public class CartServiceImpl implements CartService {

	@Autowired
	CartDao cartDao;
	@Autowired
	GoodsPriceDao goodsPriceDao;

	@Override
	public Map<String, Object> getCart(Map<String, String> filter) {
		ResultJson resultJson = JsonChangeUtil.setResultJson("0000", "请求成功");

		String userid = filter.get("userid");
		resultJson.put("data", cartDao.queryByUserId(Integer.valueOf(userid)));

		return resultJson.getResultMap();
	}

	@Override
	public Map<String, Object> deleteAllGoods(Map<String, String> filter) {
		ResultJson resultJson = JsonChangeUtil.setResultJson("0000", "请求成功");

		String userid = filter.get("userid");
		List<TGoodsCartEntity> obj = cartDao.findBy("userId", Integer.valueOf(userid));
		if(obj!=null&&!obj.isEmpty()){
			cartDao.removeAll(obj);
		}

		return resultJson.getResultMap();
	}

	@Override
	public Map<String, Object> deleteGoods(Map<String, String> filter) {
		ResultJson resultJson = JsonChangeUtil.setResultJson("0000", "请求成功");

		String userid = filter.get("userid");
		String goodsid = filter.get("goodsid");
		String priceid = filter.get("priceid");

		Map<String,Object> param = new HashMap<String, Object>();
		param.put("userId", Integer.valueOf(userid));
		param.put("goodsId", Integer.valueOf(goodsid));
		param.put("priceId", Integer.valueOf(priceid));

		List<TGoodsCartEntity> obj = cartDao.find(param);
		if(obj!=null&&!obj.isEmpty()){
			cartDao.removeAll(obj);
		}

		return resultJson.getResultMap();
	}

	@Override
	public Map<String, Object> updateGoodsCount(Map<String, String> filter) {
		ResultJson resultJson = JsonChangeUtil.setResultJson("0000", "请求成功");

		String userid = filter.get("userid");
		String goodsid = filter.get("goodsid");
		String count = filter.get("count");		// 商品数量，正数和负数。正数表示加，负数表示减
		String priceid = filter.get("priceid");		// 参数

		Map<String,Object> param = new HashMap<String, Object>();
		param.put("userId", Integer.valueOf(userid));
		param.put("goodsId", Integer.valueOf(goodsid));
		param.put("priceId", Integer.valueOf(priceid));
		TGoodsPriceEntity priceEntity = goodsPriceDao.get(Integer.valueOf(priceid));

		List<TGoodsCartEntity> objs = cartDao.find(param);
		if(objs!=null&&!objs.isEmpty()){		// 之前已经添加过了,把数量加上去
			if(objs.size()==1){
				TGoodsCartEntity entity = objs.get(0);
				entity.setGoodsNumber(entity.getGoodsNumber() + Integer.valueOf(count));
				entity.setGoodsAttr(priceEntity==null?"":priceEntity.generateAttr());
				cartDao.update(entity);
			}else{
				TGoodsCartEntity entity = objs.get(0);
				for(int i = 1;i < objs.size(); i++){
					entity.setGoodsNumber(entity.getGoodsNumber() + objs.get(i).getGoodsNumber());
				}
				entity.setGoodsNumber(entity.getGoodsNumber() + Integer.valueOf(count));
				objs.remove(0);
				cartDao.update(entity);
				cartDao.removeAll(objs);
			}
		}else{			// 之前没有添加过，添加
			TGoodsCartEntity obj = new TGoodsCartEntity();
			obj.setAddTime(new Timestamp(System.currentTimeMillis()));
			obj.setUpdateTime(obj.getAddTime());
			obj.setUserId(Integer.valueOf(userid));
			obj.setGoodsAttr(priceEntity==null?"":priceEntity.generateAttr());
			obj.setGoodsId(Integer.valueOf(goodsid));
			obj.setGoodsNumber(Integer.valueOf(count));
			obj.setPriceId(Integer.valueOf(priceid));
			cartDao.create(obj);
		}

		return resultJson.getResultMap();
	}
}
